Shader "Custom/UnderwaterEffect"
{
    Properties
    {
        _MainColor("Water Color", Color) = (0.1, 0.3, 0.5, 0.8)
        _DepthColor("Depth Color", Color) = (0.05, 0.15, 0.3, 1.0)
        _DepthPower("Depth Power", Range(0.1, 5.0)) = 1.5
        _NormalMap("Normal Map", 2D) = "bump" {}
        _NormalScale("Normal Scale", Range(0.01, 0.5)) = 0.1
        _Distortion("Distortion", Range(0.0, 0.1)) = 0.02
        _NoiseScale("Noise Scale", Float) = 0.5
        _NoiseSpeed("Noise Speed", Vector) = (0.1, 0.1, 0, 0)
        _WaveHeight("Wave Height", Range(0.0, 0.5)) = 0.1
        _WaveSpeed("Wave Speed", Range(0.1, 5.0)) = 1.0
        _WaveFrequency("Wave Frequency", Range(1.0, 10.0)) = 2.0
    }

    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off

        GrabPass { "_GrabTexture" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 grabPos : TEXCOORD1;
                float4 pos : SV_POSITION;
                float3 worldPos : TEXCOORD2;
            };

            sampler2D _NormalMap;
            float4 _NormalMap_ST;
            float _NormalScale;
            float _Distortion;
            float _NoiseScale;
            float2 _NoiseSpeed;
            sampler2D _GrabTexture;
            float4 _GrabTexture_ST;
            sampler2D _CameraDepthTexture;
            float4 _MainColor;
            float4 _DepthColor;
            float _DepthPower;
            float _WaveHeight;
            float _WaveSpeed;
            float _WaveFrequency;

            v2f vert (appdata v)
            {
                v2f o;
                // 计算波浪位移
                float wave = sin(v.vertex.x * _WaveFrequency + _Time.y * _WaveSpeed) * _WaveHeight;
                v.vertex.y += wave;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _NormalMap);
                o.grabPos = ComputeGrabScreenPos(o.pos);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // 采样法线图
                float2 noiseUV = i.uv * _NoiseScale + _Time.y * _NoiseSpeed;
                float3 normal = UnpackNormal(tex2D(_NormalMap, noiseUV));
                normal.xy *= _NormalScale;

                // 计算扭曲的抓取纹理坐标
                float2 distortedUV = i.grabPos.xy / i.grabPos.w + normal.xy * _Distortion;
                fixed4 col = tex2D(_GrabTexture, distortedUV);

                // 计算深度
                float depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, UNITY_PROJ_COORD(i.grabPos)));
                float waterDepth = depth - i.grabPos.w;
                float depthFactor = saturate(pow(waterDepth * _DepthPower, 0.5));

                // 应用深度颜色
                col.rgb = lerp(col.rgb, _DepthColor.rgb, depthFactor * _DepthColor.a);
                // 应用主颜色
                col.rgb = lerp(col.rgb, _MainColor.rgb, _MainColor.a);
                col.a = _MainColor.a;

                return col;
            }
            ENDCG
        }
    }
    FallBack "Transparent/VertexLit"
}